<template>
	<view class="container">
		<!-- 顶部统计信息 -->
		<view class="statistics-bar">
			<view class="stat-item">
				<text class="stat-number">{{totalRooms}}</text>
				<text class="stat-label">总房间数</text>
			</view>
			<view class="stat-item">
				<text class="stat-number">{{rentedRooms}}</text>
				<text class="stat-label">已出租</text>
			</view>
			<view class="stat-item">
				<text class="stat-number">{{emptyRooms}}</text>
				<text class="stat-label">空置中</text>
			</view>
		</view>
		
		<!-- 搜索栏 -->
		<view class="search-bar">
			<view class="search-input">
				<uni-icons type="search" size="18" color="#999"></uni-icons>
				<input type="text" v-model="searchText" placeholder="搜索房间号或租户姓名" />
			</view>
			<button class="add-btn" @click="addNewRoom">添加房间</button>
		</view>
		
		<!-- 房间列表 -->
		<view class="room-list">
			<view class="room-item" v-for="(room, index) in filteredRooms" :key="index" @click="goToRoomDetail(room.id)">
				<view class="room-info">
					<view class="room-header">
						<text class="room-number">{{room.roomNumber}}</text>
						<text :class="['room-status', room.status === '已出租' ? 'status-rented' : 'status-empty']">{{room.status}}</text>
					</view>
					<view class="room-address">{{room.address}}</view>
					<view class="room-tenant" v-if="room.tenant">
						<text class="tenant-label">租户：</text>
						<text class="tenant-name">{{room.tenant.name}}</text>
						<text class="tenant-phone">{{room.tenant.phone}}</text>
					</view>
					<view class="room-tenant" v-else>
						<text class="tenant-label empty-tenant">暂无租户</text>
					</view>
					<view class="room-price">
						<text class="price-value">¥{{room.price}}</text>
						<text class="price-unit">/月</text>
					</view>
				</view>
				<view class="room-action">
					<uni-icons type="right" size="16" color="#CCCCCC"></uni-icons>
				</view>
			</view>
		</view>
		
		<!-- 空状态 -->
		<view class="empty-state" v-if="rooms.length === 0">
			<image src="/static/empty.png" mode="aspectFit" class="empty-image"></image>
			<text class="empty-text">暂无房间信息，点击"添加房间"开始管理</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchText: '',
				rooms: [
					{
						id: 1,
						roomNumber: '101',
						address: '北京市海淀区西二旗大街58号院1号楼',
						status: '已出租',
						price: 3500,
						tenant: {
							name: '张三',
							phone: '13812345678'
						}
					},
					{
						id: 2,
						roomNumber: '102',
						address: '北京市海淀区西二旗大街58号院1号楼',
						status: '已出租',
						price: 3200,
						tenant: {
							name: '李四',
							phone: '13987654321'
						}
					},
					{
						id: 3,
						roomNumber: '201',
						address: '北京市海淀区西二旗大街58号院1号楼',
						status: '空置中',
						price: 3800,
						tenant: null
					},
					{
						id: 4,
						roomNumber: '202',
						address: '北京市海淀区西二旗大街58号院2号楼',
						status: '空置中',
						price: 4000,
						tenant: null
					},
					{
						id: 5,
						roomNumber: '301',
						address: '北京市海淀区西二旗大街58号院2号楼',
						status: '已出租',
						price: 4200,
						tenant: {
							name: '王五',
							phone: '13700001111'
						}
					}
				]
			};
		},
		computed: {
			totalRooms() {
				return this.rooms.length;
			},
			rentedRooms() {
				return this.rooms.filter(room => room.status === '已出租').length;
			},
			emptyRooms() {
				return this.rooms.filter(room => room.status === '空置中').length;
			},
			filteredRooms() {
				if (!this.searchText) return this.rooms;
				
				const keyword = this.searchText.toLowerCase();
				return this.rooms.filter(room => {
					return room.roomNumber.toLowerCase().includes(keyword) || 
						(room.tenant && room.tenant.name.toLowerCase().includes(keyword));
				});
			}
		},
		methods: {
			goToRoomDetail(roomId) {
				uni.navigateTo({
					url: `/pages/fangdong/roomDetail?id=${roomId}`
				});
			},
			addNewRoom() {
				// 跳转到添加房间页面
				uni.showToast({
					title: '添加房间功能开发中',
					icon: 'none'
				});
			}
		}
	}
</script>

<style lang="scss">
.container {
	padding: 20rpx;
	background-color: #f5f5f5;
	min-height: 100vh;
}

.statistics-bar {
	display: flex;
	justify-content: space-around;
	background-color: #ffffff;
	border-radius: 12rpx;
	padding: 30rpx 0;
	margin-bottom: 20rpx;
	box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	
	.stat-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		
		.stat-number {
			font-size: 36rpx;
			font-weight: bold;
			color: #333333;
			margin-bottom: 10rpx;
		}
		
		.stat-label {
			font-size: 24rpx;
			color: #999999;
		}
	}
}

.search-bar {
	display: flex;
	align-items: center;
	margin-bottom: 20rpx;
	
	.search-input {
		flex: 1;
		display: flex;
		align-items: center;
		background-color: #ffffff;
		border-radius: 30rpx;
		padding: 15rpx 20rpx;
		margin-right: 20rpx;
		
		input {
			flex: 1;
			height: 40rpx;
			margin-left: 10rpx;
			font-size: 28rpx;
		}
	}
	
	.add-btn {
		background-color: #007AFF;
		color: #ffffff;
		font-size: 28rpx;
		padding: 0 30rpx;
		height: 70rpx;
		line-height: 70rpx;
		border-radius: 35rpx;
	}
}

.room-list {
	.room-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #ffffff;
		border-radius: 12rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
		
		.room-info {
			flex: 1;
			
			.room-header {
				display: flex;
				align-items: center;
				margin-bottom: 15rpx;
				
				.room-number {
					font-size: 32rpx;
					font-weight: bold;
					color: #333333;
					margin-right: 20rpx;
				}
				
				.room-status {
					font-size: 24rpx;
					padding: 6rpx 16rpx;
					border-radius: 20rpx;
				}
				
				.status-rented {
					background-color: #e6f7ff;
					color: #1890ff;
				}
				
				.status-empty {
					background-color: #f6ffed;
					color: #52c41a;
				}
			}
			
			.room-address {
				font-size: 26rpx;
				color: #666666;
				margin-bottom: 15rpx;
			}
			
			.room-tenant {
				font-size: 26rpx;
				color: #666666;
				margin-bottom: 15rpx;
				
				.tenant-label {
					color: #999999;
				}
				
				.tenant-name {
					margin-right: 20rpx;
				}
				
				.empty-tenant {
					color: #ff9800;
				}
			}
			
			.room-price {
				.price-value {
					font-size: 30rpx;
					font-weight: bold;
					color: #f5222d;
				}
				
				.price-unit {
					font-size: 24rpx;
					color: #999999;
				}
			}
		}
	}
}

.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 100rpx 0;
	
	.empty-image {
		width: 200rpx;
		height: 200rpx;
		margin-bottom: 30rpx;
	}
	
	.empty-text {
		font-size: 28rpx;
		color: #999999;
		text-align: center;
	}
}
</style>
